{% extends "base.html" %}
{% block content %}
<h2>Patients</h2>

<!-- Search -->
<form method="get" class="inline-form">
  <input name="name" placeholder="Search by full name" value="{{ name }}" />
  <button>Search</button>
</form>

<!-- Create -->
<section class="card">
  <h3>Create Patient</h3>
  <form method="post" action="{{ url_for('create_patient') }}" class="form-grid">
    <input name="first_name" placeholder="First name" required />
    <input name="last_name" placeholder="Last name" required />
    <input name="date_of_birth" type="date" required />
    <input name="gender" placeholder="Gender" />
    <input name="email" placeholder="Email" />
    <input name="phone" placeholder="Phone" />
    <input name="address" placeholder="Address" />
    <input name="emergency_contact" placeholder="Emergency contact" />
    <button type="submit">Create</button>
  </form>
</section>

<!-- Edit panel (if any) -->
{% if edit_patient %}
<section class="card">
  <h3>Edit Patient #{{ edit_patient.patient_id }}</h3>
  <form method="post" action="{{ url_for('edit_patient', patient_id=edit_patient.patient_id) }}" class="form-grid">
    <input name="first_name" value="{{ edit_patient.first_name }}" />
    <input name="last_name" value="{{ edit_patient.last_name }}" />
    <input name="date_of_birth" type="date" value="{{ edit_patient.date_of_birth }}" />
    <input name="gender" value="{{ edit_patient.gender }}" />
    <input name="email" value="{{ edit_patient.email }}" />
    <input name="phone" value="{{ edit_patient.phone }}" />
    <input name="address" value="{{ edit_patient.address }}" />
    <input name="emergency_contact" value="{{ edit_patient.emergency_contact }}" />
    <button type="submit">Save</button>
  </form>
</section>
{% endif %}

<!-- Table -->
<section class="card">
  <h3>Patients List ({{ patients|length }})</h3>
  <table class="striped">
    <thead>
      <tr><th>ID</th><th>Name</th><th>Email</th><th>Phone</th><th>Actions</th></tr>
    </thead>
    <tbody>
      {% for p in patients %}
      <tr>
        <td>{{ p.patient_id }}</td>
        <td>{{ p.first_name }} {{ p.last_name }}</td>
        <td>{{ p.email or "-" }}</td>
        <td>{{ p.phone or "-" }}</td>
        <td class="actions">
          <a class="btn" href="{{ url_for('edit_patient', patient_id=p.patient_id) }}">Edit</a>
          <form method="post" action="{{ url_for('delete_patient', patient_id=p.patient_id) }}" style="display:inline">
            <button class="btn danger" type="submit" onclick="return confirm('Delete patient #{{ p.patient_id }}?')">Delete</button>
          </form>
        </td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</section>
{% endblock %}
